package TestSynchronized;

import org.junit.jupiter.api.Test;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author Mr.Wen
 * @Version 1.0
 */
public class Test2 {
    final ReentrantLock lock = new ReentrantLock();

    @Test
    void test1() {
        new Thread(() -> {
            lock.lock();
            try {
                System.out.println("t1拿锁");
                try {
                    lock.lock();
                    System.out.println("t1拿锁no.2");
                } finally {
//                    lock.unlock();/*如果不释放,重入锁计数器不为0,t2就无法拿锁*/
                }
            } finally {
                lock.unlock();
            }
        }, "t1").start();
        new Thread(() -> {
            try {
                lock.lock();
                System.out.println("t2拿锁");
            } finally {
                lock.unlock();
            }
        }, "t2").start();

    }
}
